package com.cyj.springbootservice.service.impl;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyj.springbootservice.api.entity.SysDept;
import com.cyj.springbootservice.api.entity.SysDeptRelation;
import com.cyj.springbootservice.mapper.SysDeptMapper;
import com.cyj.springbootservice.service.SysDeptRelationService;
import com.cyj.springbootservice.service.SysDeptService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @Author C_Y_J
 * @create 2022/5/10 10:22
 **/
@Service
@AllArgsConstructor
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements SysDeptService {

    private final SysDeptMapper deptMapper;

    private final SysDeptRelationService sysDeptRelationService;

    /**
     * 查询全部部门树
     *
     * @return 树
     */
    @Override
    public List<Tree<Integer>> selectTree() {
        // 查询全部部门
        List<SysDept> deptAllList = deptMapper.selectList(Wrappers.emptyWrapper());

        // 权限内部门
        List<TreeNode<Integer>> collect = deptAllList.stream()
                .filter(dept -> dept.getDeptId().intValue() != dept.getParentId())
                .sorted(Comparator.comparingInt(SysDept::getSort))
                .map(dept -> {
                    TreeNode<Integer> treeNode = new TreeNode();
                    treeNode.setId(dept.getDeptId());
                    treeNode.setParentId(dept.getParentId());
                    treeNode.setName(dept.getName());
                    treeNode.setWeight(dept.getSort());
                    HashMap<String, Object> map = MapUtil.of("扩展字段", "关注兔子不吃窝边曹");
                    map.put("custom","自定义内容");
                    treeNode.setExtra(map);
                    return treeNode;
                }).collect(Collectors.toList());
        System.out.println(collect.toString());

        return TreeUtil.build(collect, 0);
    }

    /**
     * 添加信息部门
     *
     * @param dept 部门
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveDept(SysDept dept) {
        this.save(dept);
        sysDeptRelationService.insertDeptRelation(dept);
        return Boolean.TRUE;
    }

    /**
     * 更新部门
     *
     * @param sysDept 部门信息
     * @return 成功、失败
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateDeptById(SysDept sysDept) {
        // 更新部门状态
        this.updateById(sysDept);
        // 更新部门关系
        SysDeptRelation relation = new SysDeptRelation();
        relation.setAncestor(sysDept.getParentId());
        relation.setDescendant(sysDept.getDeptId());
        sysDeptRelationService.updateDeptRealtion(relation);
        return Boolean.TRUE;
    }

    /**
     * 删除部门
     *
     * @param id 部门 ID
     * @return 成功、失败
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeDeptById(Integer id) {
        // 级联删除部门
        List<Integer> idList = sysDeptRelationService.lambdaQuery().eq(SysDeptRelation::getAncestor, id).list()
                .stream().map(SysDeptRelation::getDescendant).collect(Collectors.toList());

        if (CollUtil.isNotEmpty(idList)) {
            this.removeByIds(idList);
        }

        // 删除部门级联关系
        sysDeptRelationService.deleteAllDeptRealtion(id);
        return Boolean.TRUE;
    }

}

